<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AssessType;
use Illuminate\Http\Request;
use App\Models\Grid;
use App\Models\GridAssess;
use App\Models\ApprovalSet;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 评估
class GridAssessController extends Controller
{
    // 录入
    public function addAssess(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'grid_id' => 'required',
            'total_price' => 'required',
            'unit_price' => 'required',
            'assess_imgs' => 'required',
        ], [
            'grid_id.required' => '请输入网格id',
            'total_price.required' => '请输入总价',
            'unit_price.required' => '请输入单价',
            'assess_imgs.required' => '请输入照片',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();
        $Info = $request->all();
        isset($Info['remarks'])?$params['remarks']=$Info['remarks']:'';
        isset($Info['attach_fee'])?$params['attach_fee']=$Info['attach_fee']:'';
        isset($Info['stop_fee'])?$params['stop_fee']=$Info['stop_fee']:'';
        isset($Info['subsidy_fee'])?$params['subsidy_fee']=$Info['subsidy_fee']:'';
        isset($Info['content'])?$params['content']=$Info['content']:'';

        $params['do_user_id']=auth('api')->user()->id??0;
        try {
            $data = GridAssess::query()
            ->updateOrCreate(['grid_id'=>$params['grid_id']],$params);

            // 处理完成状态
            $grid=Grid::query()->select('project_id','street_id','village_id')->where('id',$params['grid_id'])->first();
            $approval_set=ApprovalSet::query()
            ->where('project_id',$grid['project_id']??2)
            ->where('street_id',$grid['street_id']??3)
            ->where('village_id',$grid['village_id']??1)
            ->where('template_id',27)
            ->where('status',1)
            ->value('id');
            if($approval_set){//有审批
                Grid::query()->where('id',$params['grid_id'])->update(['assess_status'=>2]);

                // 触发审批-完成评估
                if(!$this->findApproval($params['grid_id'], 27)){
                    $this->setApproval($params['grid_id'],27);
                }
            }else{
                Grid::query()->where('id',$params['grid_id'])->update(['assess_status'=>1]);
            }    

            return showSuccess('提交成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
       
    }

    // 回显
    public function assessList(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'grid_id' => 'required',
        ], [
            'grid_id.required' => '请输入网格id',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        $data=GridAssess::query()
        ->where('grid_id',$params['grid_id'])
        ->first();

        if($data){
           $data['assess_imgs']=$data['assess_imgs']?$this->getImageUrl($data['assess_imgs'],1):[];
           $content=json_decode($data['content'],true);
        //    foreach($content as &$v){
        //       $v['assess_type']=AssessType::query()
        //                 ->select('name','unit','pid')
        //                 ->where('id',$v['assess_type_id'])
        //                 ->first()
        //                 ->toArray();
        //       $v['assess_type']['pid_name']=AssessType::query()
        //                 ->select('name','unit','pid')
        //                 ->where('id',$v['assess_type']['pid'])
        //                 ->value('name');     
        //    }
           $data['content']=$content;
        }

        $grid=Grid::query()->select('house_use','if_sign','for_user_id')->where('id',$params['grid_id'])->first();
        $data['house_use']=$grid['house_use'];
        $data['if_sign']=$grid['if_sign'];
        $data['for_user_id']=$grid['for_user_id'];
        return showData($data);
    }
}

